/**
 * @Authoer: hujun
 * @Description: 金融机构管理
 * @Date 2017/12/5 10:41
 * @Modified By:
 *
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/content/systemManager/fincalOrgManager/fincalOrgManager.js'
], function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    // 注册该功能要用到的数据字典
    yufp.lookup.reg('DATA_STS,SYS_TYPE');
    // 创建virtual model
    yufp.custom.vue({
      el: '#fincalOrg',
      // 以m_开头的属性为UI数据不作为业务数据，否则为业务数据
      data: function () {
        var me = this;
        return {
          formdata: {},
          height: yufp.custom.viewSize().height - 150,
          createButton: !yufp.session.checkCtrl('addFincalOrg'), // 新增按钮控制
          editButton: !yufp.session.checkCtrl('editFincalOrg'), // 修改按钮控制
          deleteButton: !yufp.session.checkCtrl('deleteFincalOrg'), // 删除按钮控制
          useButton: !yufp.session.checkCtrl('useFincalOrg'), // 启用按钮控制
          unuseButton: !yufp.session.checkCtrl('unuseFincalOrg'), // 停用按钮控制
          datePick: {// 限制不可选择之前的日期
            disabledDate: function (time) {
              return time.getTime() < Date.now() - 8.64e7;
            }
          },
          mainGrid: {
            query: {
              sysId: '',
              instuCde: '',
              instuName: '',
              instuSts: ''
            },
            height: yufp.custom.viewSize().height - 220,
            checkbox: true,
            dataUrl: backend.appOcaService + '/api/adminsminstu/querypage',
            paging: {
              page: 1,
              size: 10
            },
            currentRow: null,
            tableColumns: [
              {
                label: this.$t('fincalOrgManager.jrjgmc'),
                prop: 'instuName',
                resizable: true,
                template: function (data) {
                  return '<template scope="scope">\
                                <a style="text-decoration:underline;cursor:pointer;" class="underline" @click="_$event(\'custom-detail-click\', scope)">{{scope.row.instuName}}</a>\
                            </template>';
                }
              }, {
                label: this.$t('fincalOrgManager.jrjgdm'),
                prop: 'instuCde',
                resizable: true
              }, {
                label: this.$t('fincalOrgManager.zt'),
                prop: 'instuSts',
                resizable: true,
                template: function () {
                  return '<template scope="scope">\
                  <yu-tag type="success" v-if="scope.row.instuSts == \'A\' ">生效</yu-tag>\
                  <yu-tag type="warning" v-if="scope.row.instuSts == \'W\' ">待生效</yu-tag>\
                  <yu-tag type="danger" v-if="scope.row.instuSts == \'I\' ">失效</yu-tag>\
                  </template>';
                }    
              }, {
                label: this.$t('fincalOrgManager.jrrq'),
                prop: 'joinDt',
                type: 'date',
                resizable: true
              }, {
                label: this.$t('fincalOrgManager.zxbgyh'),
                prop: 'userName',
                resizable: true
              }, {
                label: this.$t('fincalOrgManager.zxbgsj'),
                prop: 'lastChgDt',
                type: 'date',
                resizable: true
              }
            ]
          },
          updateFields: [{
            columnCount: 2,
            fields: [
              {
                field: 'instuCde',
                label: this.$t('fincalOrgManager.jrjgdm'),
                rules: [{
                  required: true,
                  message: this.$t('fincalOrgManager.btx'),
                  trigger: 'blur'
                }, {
                  max: 10,
                  message: this.$t('fincalOrgManager.zdcdbcggzf'),
                  trigger: 'blur'
                }, { validator: yufp.validator.speChar, message: this.$t('fincalOrgManager.srxxbhtszf'), trigger: 'blur' }]
              }, {
                field: 'instuName',
                label: this.$t('fincalOrgManager.jrjgmc'),
                rules: [{
                  required: true,
                  message: this.$t('fincalOrgManager.btx'),
                  trigger: 'blur'
                }, {
                  max: 100,
                  message: this.$t('fincalOrgManager.zdcdbcgghz'),
                  trigger: 'blur'
                }, { validator: yufp.validator.speChar, message: this.$t('fincalOrgManager.srxxbhtszf'), trigger: 'blur' }]
              }, {
                field: 'instuSts',
                label: this.$t('fincalOrgManager.zt'),
                type: 'select',
                dataCode: 'DATA_STS',
                disabled: true
              }
            ]
          }],
          updateOtherFields: [{
            columnCount: 2,
            fields: [{
              field: 'joinDt',
              label: this.$t('fincalOrgManager.jrrq'),
              type: 'date',
              format: 'yyyy-MM-dd',
              pickerOptions: me.datePick
            }, {
              field: 'zipCde',
              label: this.$t('fincalOrgManager.yb'),
              rules: [{
                validator: yufp.validator.postcode,
                message: this.$t('fincalOrgManager.qsrzqxx'),
                trigger: 'blur'
              }]
            }, {
              field: 'contUsr',
              label: this.$t('fincalOrgManager.lxr'),
              rules: [{
                max: 100,
                message: this.$t('fincalOrgManager.zdcdbcgghz'),
                trigger: 'blur'
              }]
            }, {
              field: 'contTel',
              label: this.$t('fincalOrgManager.lxdh'),
              rules: [{
                validator: yufp.validator.mobile,
                message: this.$t('fincalOrgManager.qsrzqxx'),
                trigger: 'blur'
              }]
            }, {
              field: 'userName',
              label: this.$t('fincalOrgManager.zxbgyh')
            }, {
              field: 'lastChgDt',
              label: this.$t('fincalOrgManager.zxbgsj')
            }]
          }, {
            columnCount: 1,
            fields: [{
              field: 'instuAddr',
              label: this.$t('fincalOrgManager.dz'),
              type: 'textarea',
              rows: 1,
              rules: [{
                max: 200,
                message: this.$t('fincalOrgManager.zdcdbcgghz'),
                trigger: 'blur'
              }]
            }]
          }],
          dialogFormVisible: false,
          dialogStatus: '',
          formDisabled: false,
          nowRow: null,
          dialogDisabled: true,
          activeName: ['1', '2'], // 默认显示name为1的
          textMap: {
            update: this.$t('fincalOrgManager.xg'),
            detail: this.$t('fincalOrgManager.xq'),
            create: this.$t('fincalOrgManager.xz')
          }
        };
      },
      mounted: function () {
        this.queryFn();
      },
      methods: {
        queryFn: function () {
          var me = this;
          me.$refs.infotable.remoteData();
        },
        openCreateFn: function () {
          var _this = this;
          this.opType('create', false, ['1', '2']);
          this.updateFields[0].fields[0].readonly = false;
          this.updateOtherFields[0].fields[4].hidden = true;
          this.updateOtherFields[0].fields[5].hidden = true;
          this.updateOtherFields[0].fields[0].pickerOptions = this.datePick;
          this.$nextTick(function () {
            _this.$refs.form.resetFn();
            _this.$refs.otherForm.resetFn();
            _this.$refs.form.formModel.instuSts = 'W';
            _this.$refs.otherForm.formModel.joinDt = new Date();
          });
        },
        openDetailFn: function (row) {
          this.opType('detail', true, ['1', '2']);
          this.updateOtherFields[0].fields[4].hidden = false;
          this.updateOtherFields[0].fields[5].hidden = false;
          this.$nextTick(function () {
            this.$refs.form.resetFn();
            this.$refs.otherForm.resetFn();
            yufp.extend(this.$refs.form.formModel, row.row);
            yufp.extend(this.$refs.otherForm.formModel, row.row);
          });
        },
        openEditFn: function () {
          if (this.$refs.infotable.selections.length !== 1) {
            this.$message({
              message: this.$t('fincalOrgManager.qxzytsj'),
              type: 'warning'
            });
            return false;
          }
          var row = this.$refs.infotable.selections[0];
          if (row.joinDt == '' || row.joinDt == null) {
            row.joinDt = new Date();
          }
          if (row.instuSts === 'A') {
            this.$message({
              message: this.$t('fincalOrgManager.znxztyhdqydsj'),
              type: 'warning'
            });
            return;
          }
          this.opType('update', false, ['1', '2']);
          this.updateFields[0].fields[0].readonly = true;
          this.updateOtherFields[0].fields[4].hidden = true;
          this.updateOtherFields[0].fields[5].hidden = true;
          this.updateOtherFields[0].fields[0].pickerOptions = this.datePick;
          this.$nextTick(function () {
            this.$refs.form.resetFn();
            this.$refs.otherForm.resetFn();
            yufp.extend(this.$refs.form.formModel, row);
            yufp.extend(this.$refs.otherForm.formModel, row);
          });
        },
        opType: function (type, disabled, active) {
          this.dialogFormVisible = true;
          this.dialogStatus = type;
          this.activeName = active; // 显示哪一块
          this.formDisabled = disabled;
        },
        saveCreateFn: function () {
          var vue = this;
          var form = vue.$refs.form,
            otherForm = vue.$refs.otherForm;
          var formFlag = true;
          var otherFormFlag = true;
          form.validate(function (valid) {
            formFlag = valid;
          });
          otherForm.validate(function (valid) {
            otherFormFlag = valid;
          });
          if (formFlag && otherFormFlag) {
            var comitData = {
              contTel: null,
              contUsr: null,
              instuAddr: null,
              joinDt: null,
              zipCde: null
            };
            delete form.formModel.instuId;
            form.formModel.sysId = yufp.session.logicSys.id;
            yufp.extend(comitData, form.formModel);
            if (otherForm.formModel.joinDt != undefined) {
              comitData.joinDt = otherForm.formModel.joinDt;
            }
            comitData.zipCde = otherForm.formModel.zipCde;
            comitData.contUsr = otherForm.formModel.contUsr;
            comitData.contTel = otherForm.formModel.contTel;
            comitData.instuAddr = otherForm.formModel.instuAddr;
            comitData.lastChgUsr = yufp.session.userId;
            comitData.sysId = yufp.session.logicSys.id;
            yufp.service.request({
              method: 'POST',
              url: backend.appOcaService + '/api/adminsminstu/insertinfo',
              data: comitData,
              callback: function (code, message, response) {
                if (response.data.code == '2') {
                  vue.$message({
                    message: response.data.message,
                    type: 'warning'
                  });
                } else {
                  vue.dialogFormVisible = false;
                  vue.$message({
                    message: vue.$t('fincalOrgManager.sjbccg')
                  });
                  vue.queryFn();
                }
              }
            });
          } else {
            this.$message({
              message: this.$t('fincalOrgManager.qjcsrxsfhf'),
              type: 'warning'
            });
            return false;
          }
        },
        saveEditFn: function () {
          var me = this;
          var form = me.$refs.form,
            otherForm = me.$refs.otherForm;
          var formFlag = true;
          var otherFormFlag = true;
          form.validate(function (valid) {
            formFlag = valid;
          });
          otherForm.validate(function (valid) {
            otherFormFlag = valid;
          });
          if (formFlag && otherFormFlag) {
            var comitData = {
              contTel: null,
              contUsr: null,
              instuAddr: null,
              joinDt: null,
              zipCde: null
            };
            yufp.extend(comitData, form.formModel);
            comitData.joinDt = otherForm.formModel.joinDt;
            comitData.zipCde = otherForm.formModel.zipCde;
            comitData.contUsr = otherForm.formModel.contUsr;
            comitData.contTel = otherForm.formModel.contTel;
            comitData.instuAddr = otherForm.formModel.instuAddr;
            comitData.lastChgUsr = yufp.session.userId;
            comitData.sysId = yufp.session.logicSys.id;
            yufp.service.request({
              method: 'POST',
              url: backend.appOcaService + '/api/adminsminstu/update',
              data: comitData,
              callback: function (code, message, response) {
                me.dialogFormVisible = false;
                me.$message({
                  message: me.$t('fincalOrgManager.sjbccg')
                });
                me.queryFn();
              }
            });
          } else {
            this.$message({
              message: this.$t('fincalOrgManager.qjcsrxsfhf'),
              type: 'warning'
            });
            return false;
          }
        },
        useFn: function () { // 启用
          if (this.$refs.infotable.selections.length > 0) {
            var id = '';
            for (var i = 0; i < this.$refs.infotable.selections.length; i++) {
              var row = this.$refs.infotable.selections[i];
              if (row.instuSts !== 'A') {
                id = id + ',' + row.instuId;
              } else {
                this.$message({
                  message: this.$t('fincalOrgManager.znxztyhzdsxdsj'),
                  type: 'warning'
                });
                return false;
              }
            }
            var vue = this;
            this.$confirm(this.$t('fincalOrgManager.cczjqygsjsfjx'), this.$t('fincalOrgManager.ts'), {
              confirmButtonText: this.$t('fincalOrgManager.qr'),
              cancelButtonText: this.$t('fincalOrgManager.qx'),
              type: 'warning'
            }).then(function () {
              yufp.service.request({
                method: 'POST',
                data: {
                  id: id,
                  lastChgUsr: yufp.session.userId
                },
                url: backend.appOcaService + '/api/adminsminstu/usebatch?ids=' + id + '&lastChgUsr=' + yufp.session.userId,
                callback: function (code, message, response) {
                  vue.$message({
                    message: vue.$t('fincalOrgManager.sjczcg')
                  });
                  vue.queryFn();
                }
              });
            });
          } else {
            this.$message({
              message: this.$t('fincalOrgManager.qxxzysxdsj'),
              type: 'warning'
            });
            return;
          }
        },
        unUseFn: function () { // 停用
          if (this.$refs.infotable.selections.length > 0) {
            var id = '';
            for (var i = 0; i < this.$refs.infotable.selections.length; i++) {
              var row = this.$refs.infotable.selections[i];
              if (row.instuSts === 'A') {
                id = id + ',' + row.instuId;
              } else {
                this.$message({
                  message: this.$t('fincalOrgManager.znxzsxdsj'),
                  type: 'warning'
                });
                return false;
              }
            }

            var vue = this;
            this.$confirm(this.$t('fincalOrgManager.cczjtygsjsfjx'), this.$t('fincalOrgManager.ts'), {
              confirmButtonText: this.$t('fincalOrgManager.qr'),
              cancelButtonText: this.$t('fincalOrgManager.qx'),
              type: 'warning'
            }).then(function () {
              yufp.service.request({
                method: 'POST',
                data: {
                  id: id,
                  lastChgUsr: yufp.session.userId
                },
                url: backend.appOcaService + '/api/adminsminstu/unusebatch?ids=' + id + '&lastChgUsr=' + yufp.session.userId,
                callback: function (code, message, response) {
                  if (code == 0) {
                    vue.$message({
                      message: response.message
                    });
                    vue.queryFn();
                  }
                }
              });
            });
          } else {
            this.$message({
              message: this.$t('fincalOrgManager.qxxzysxdsj'),
              type: 'warning'
            });
            return false;
          }
        },
        deletestFn: function () { // 删除
          if (this.$refs.infotable.selections.length > 0) {
            var id = '';
            for (var i = 0; i < this.$refs.infotable.selections.length; i++) {
              var row = this.$refs.infotable.selections[i];
              if (row.instuSts !== 'A') {
                id = id + ',' + row.instuId;
              } else {
                this.$message({
                  message: this.$t('fincalOrgManager.znscsxhzdsxdsj'),
                  type: 'warning'
                });
                return false;
              }
            }
            var vue = this;
            this.$confirm(this.$t('fincalOrgManager.cczjyjscgwjsfjx'), this.$t('fincalOrgManager.ts'), {
              confirmButtonText: this.$t('fincalOrgManager.qr'),
              cancelButtonText: this.$t('fincalOrgManager.qx'),
              type: 'warning'
            }).then(function () {
              yufp.service.request({
                method: 'POST',
                url: backend.appOcaService + '/api/adminsminstu/deletebatch?ids=' + id,
                callback: function (code, message, response) {
                  if (response != null) {
                    vue.$message({
                      message: response.message
                    });
                    vue.queryFn();
                  }
                }
              });
            });
          } else {
            this.$message({
              message: this.$t('fincalOrgManager.qxxzyscdsj'),
              type: 'warning'
            });
            return false;
          }
        }
      }
    });
  };

  // 消息处理
  exports.onmessage = function (type, message) {


  };

  // page销毁时触发destroy方法
  exports.destroy = function (id, cite) {

  };
});
